//对于请求的封装

// 1.第一种封装方案

// import axios from 'axios'
// function http(params) {
//   return axios({
//     url: `https://m.maizuo.com/gateway?filmId=${params}&k=9447117`,
//     headers: {
//       'X-Client-Info':
//         '{"a":"3000","ch":"1002","v":"5.2.1","e":"16750839444888411517222913","bc":"340800"}',
//       'X-Host': 'mall.film-ticket.film.info'
//     }
//   })
// }
// export default http

// 2.第二种封装方案
import axios from 'axios'
import { Toast } from 'vant'
const http = axios.create({
  baseURL: 'https://m.maizuo.com',
  timeout: 1000
})

// 在发请求之前拦截-----show loading
http.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
      duration: 0
    })
    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)
//  在成功后拦截 ------ hide loading
http.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    Toast.clear()
    return response
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    Toast.clear()
    return Promise.reject(error)
  }
)
export default http
